package com.adjy.pathbuilders.objects;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.contacts.Contact;

import com.adjy.pathbuilders.physics.MovementType;
import com.adjy.pathbuilders.physics.PhysicalObject;
import com.adjy.pathbuilders.rendering.AnimationPack;
import com.adjy.pathbuilders.sounds.SoundManager;
import com.adjy.pathbuilders.world.GameEntity;
import com.adjy.pathbuilders.world.MainChar;

/**
 * Box class implements a GameEntity Box<br>
 * This Entity can act like a wall if hit from the sides or like a floor if hit
 * from top
 */
public class Box extends GameEntity {
	
	public static final float[] SIZE = {4, 4};

	/**
	 * Creates a Box
	 * 
	 * @param x
	 *            X Position
	 * @param y
	 *            Y Position
	 * @param frame
	 *            Frame to be used for rendering
	 */
	public Box(float x, float y, AnimationPack ap) {
		super(GameEntityType.BOX, x, y, SIZE[0], SIZE[1], ap, true, true, true);
		setAnimation("box");
	}

	@Override
	public void hasStartedCollidingWith(PhysicalObject pObject, Contact contact) {
		
		if (pObject == null)
			return;

		if (((GameEntity) pObject).getType() == GameEntityType.MAINCHAR) { // Collided
																			// with
																			// Player
			MainChar c = (MainChar) pObject;
			SoundManager.getInstance().playSoundEffect("wall");
			
			Vec2 collisionLocation = contact.getManifold().localPoint;
			for (int i = 0 ; i < contact.getManifold().pointCount ; i++) {
				collisionLocation = contact.getManifold().points[i].localPoint;
				System.out.println("ColisionLocation: " + collisionLocation.x + ", " + collisionLocation.y);
			}

			Vec2 normalizedNormal = contact.m_manifold.localNormal;
			if (normalizedNormal.y == 0) { // If Collision normal has not a Y component... (Not Vertical)
				switch (c.getMovementType()) {
				case MOVING_RIGHT:
					c.setMovementType(MovementType.MOVING_LEFT);
					break;
				case MOVING_LEFT:
					c.setMovementType(MovementType.MOVING_RIGHT);
				default:
					break;
				}
			}
		}
	}
}
